package com.tanhua.locationmanager.controller;

import com.tanhua.domain.db.Admin;
import com.tanhua.locationmanager.service.LoginService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@Controller
@RequestMapping("/login")
public class LoginController {

    private final LoginService loginService;

    public LoginController(LoginService loginService) {
        this.loginService = loginService;
    }

    @PostMapping(value = "/passwordLogin",name = "用户密码登录")
    @ResponseBody
    public ResponseEntity<?> passwordLogin(@RequestBody Map<String,String> map) {
        String username = map.get("username");
        String password = map.get("password");
        String verificationCode = map.get("verificationCode");
        String uuid = map.get("uuid");
        return loginService.passwordLogin(username,password,verificationCode,uuid);
    }

    @RequestMapping(value = "/wechatlogin",name = "微信登录，根据code获取用户信息")
    public String wechatLogin(String code, RedirectAttributes redirectAttributes) {
        return loginService.wechatLogin(code,redirectAttributes);
    }

    /**
     * @param imageBase64 图像base64
     * @return {@link ResponseEntity<?> }
     * @throws IOException ioexception
     * @Author Huo
     * @date 2021/07/17 11:07:56
     */
    @PostMapping(value = "/faceLogin",name = "人脸识别登录")
    @ResponseBody
    public ResponseEntity<?> faceLogin(MultipartFile imageBase64) throws IOException {
        return loginService.faceLogin(imageBase64);
    }

    @GetMapping(value = "/verification",name = "获取验证码")
    @ResponseBody
    public ResponseEntity<?> verification(String uuid, HttpServletResponse response) throws IOException {
        return loginService.verification(uuid,response);
    }

    @GetMapping(value = "/logout",name = "用户登出")
    @ResponseBody
    public ResponseEntity<?> logout() {
        return loginService.logout();
    }

    @PostMapping(value = "/wechatBind",name = "绑定微信")
    public ResponseEntity<?> bind(@RequestBody Map<String,String> map) {
        String username = map.get("username");
        String password = map.get("password");
        return loginService.bind(username,password);
    }

}
